package com.example.Cairodio;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;


/**
 * This activity is concerned with taking a photo for the tourist next 
 * to any of the touristic sites, and putting it into a postcard.  
 * @author AnwaRowers
 *
 */
public class TouristicPostCard extends Activity {
	private static final int CAMERA_PIC_REQUEST = 1337;  
	private static final int UPLOAD_REQUEST = 1200;
	private String place; 
    //String[] permissions = getResources().getStringArray(R.array.permissions);
    Bitmap bitmap; 
	MyPhotoShop photoshop;
	//String[] permissions = { "offline_access", "publish_stream", "user_photos", "publish_checkins",
    //  "photo_upload"};
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.postcard_view);
        //Name passed to be stated on the post card. 
        Bundle b = getIntent().getExtras();
        place = b.getString("placeName");
        photoshop = new MyPhotoShop(this);

        }
        
    
    /**
     *  starts the camera and sends back the resulting image to the application
     *  to be fit into the post card.
     * @param view
     */
    public void onClickCapture(View view) {    	
    	Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
   	startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  
    	Toast.makeText(this, getString(R.string.cam_prepare), 1).show();
    	
    }
    
    /**
     *  initiates the connection with Facebook 
     * @param view
     */
    public void onClickFace(View view) {
    	Toast.makeText(this, getString(R.string.facebook_connect), 1).show();
    	Intent intent = new Intent().setClass(TouristicPostCard.this,
				FacebookConnection.class);
    	intent.putExtra("BitmapImage", bitmap);
		startActivityForResult(intent, UPLOAD_REQUEST);
    	
    }
    
    
    public void  loadWebPage(String address) {
    	if(!address.equals(""))
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(address)));
    }
    
    /**
     * Creates an instance of the Gallery Activity.
     * @param view
     */
    public void openGallery(View view) {
    	Intent intent = new Intent().setClass(TouristicPostCard.this,
				MyGallery.class);
    	startActivity(intent);
    }
    
    /**
     * Receives the resulting image from the camera, to add the necessary 
     * effects to it. 
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
 //       try {
    	if (requestCode == CAMERA_PIC_REQUEST) { 
        	Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        	ImageView imgView = (ImageView) findViewById(R.id.imageView);   	
        	bitmap = photoshop.makeCard(thumbnail);
        	try {
				photoshop.writeImage(bitmap, place + ".jpg");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				Toast.makeText(this, e.toString(), 2).show();
				
			}
        	imgView.setImageBitmap(bitmap);
        	ImageButton fbshare = (ImageButton)findViewById(R.id.shareButton);
        	fbshare.setVisibility(View.VISIBLE);
        } else if(requestCode == UPLOAD_REQUEST) {
        	if(resultCode == Activity.RESULT_OK) {
        		Toast.makeText(this, getString(R.string.upload_success), 1).show();
        	}
        }

    }
    
    public String getPlace() {
    	return this.place;
    }
    
    }
    
    
    
    
